package com.recommend.controller.admin;

import java.util.ArrayList;
import java.util.List;

import com.recommend.domain.EvaluateDomain;
import com.recommend.dto.admin.CommentEvaluateDto;
import com.recommend.service.EvaluateService;
import org.jboss.logging.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.recommend.domain.CommentDomain;
import com.recommend.dto.admin.Comment;
import com.recommend.dto.admin.CommentDto;
import com.recommend.dto.admin.CustomerComment;
import com.recommend.service.CommentService;
import com.recommend.service.MovieService;
import com.recommend.service.UserService;

/**
 * Created by lw.
 */
@RestController
@RequestMapping("/admin/comment")
public class CommentController {
    private final static Logger logger = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private CommentService commentService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private UserService userService;
    @Autowired
    private EvaluateService evaluateService;

    @RequestMapping(value = "/getAllComments", method = RequestMethod.POST)
    @ResponseBody
    public List<CommentDto> getAllComments(){
        logger.info("==============================get all comments");
        List<Integer> movies = commentService.getAllMovie();  //电影id列表
        logger.debug("==============================comment中movies: {}=============", movies);
        List<CommentDto> commentDtos = new ArrayList<CommentDto>();
        for(Integer movie : movies){
            List<CustomerComment> customerComments = new ArrayList<CustomerComment>();
            List<Integer> customers = commentService.getCustomer(movie);
            String movieTitle = movieService.getMovie(movie).getMovieTitle();
            logger.debug("=================movie_id: {}, movie_title:{}, customer:{}=============", movie, movieTitle, customers);
            CommentDto commentDto = new CommentDto();
            for(Integer customer : customers){
                CommentDomain commentDomain = new CommentDomain();
                commentDomain.setMovieId(movie);
                commentDomain.setCustomerId(customer);
                List<Comment> commentList = commentService.getComment(commentDomain);
                logger.info("===============movie:{}, customer:{}, comment count: {}", movie, customer, commentList.size());
                CustomerComment customerComment = new CustomerComment();
                customerComment.setCustomerId(customer);
                customerComment.setName(userService.getUser(customer).getName());
                customerComment.setComments(commentList);
                customerComments.add(customerComment);
                logger.debug("==============================customer_id: {}, comment count:{}=============", customer, customerComment.getComments().size());
            }
            commentDto.setMovieId(movie);
            commentDto.setMovieTitle(movieTitle);
            commentDto.setCustomerComments(customerComments);
            commentDtos.add(commentDto);
        }
        logger.info("commentDtos count=========={}", commentDtos.size());
        return commentDtos;
    }

    @RequestMapping(value = "/getCommentsSelective", method = RequestMethod.POST)
    @ResponseBody
    public List<CommentDto> getCommentsSelective(Comment comment){
        logger.info("==============================get comments selective");
        List<Integer> movies = commentService.getMovieSelective(comment);  //电影id列表
        logger.info("==============================comment中movies: {}=============", movies);
        List<CommentDto> commentDtos = new ArrayList<CommentDto>();
        for(Integer movie : movies){
            List<CustomerComment> customerComments = new ArrayList<CustomerComment>();
            Comment comment1 = comment;
            comment1.setMovieId(movie);
            logger.info("comment: movie_id:{}, name:{}", comment.getMovieId(), comment.getName());
            List<Integer> customers = commentService.getCustomerByMovieSelective(comment1);
            String movieTitle = movieService.getMovie(movie).getMovieTitle();
            logger.info("=================movie_id: {}, movie_title:{}, customer:{}=============", movie, movieTitle, customers);
            CommentDto commentDto = new CommentDto();
            for(Integer customer : customers){
                CommentDomain commentDomain = new CommentDomain();
                commentDomain.setMovieId(movie);
                commentDomain.setCustomerId(customer);
                List<Comment> commentList = commentService.getComment(commentDomain);
                logger.info("===============movie:{}, customer:{}, comment count: {}", movie, customer, commentList.size());
                CustomerComment customerComment = new CustomerComment();
                customerComment.setCustomerId(customer);
                customerComment.setName(userService.getUser(customer).getName());
                customerComment.setComments(commentList);
                customerComments.add(customerComment);
                logger.info("==============================customer_id: {}, comment count:{}=============", customer, customerComment.getComments().size());
            }
            commentDto.setMovieId(movie);
            commentDto.setMovieTitle(movieTitle);
            commentDto.setCustomerComments(customerComments);
            commentDtos.add(commentDto);
        }
        logger.info("commentDtos count=========={}", commentDtos.size());
        return commentDtos;
    }

    @RequestMapping(value = "/deleteComment", method = RequestMethod.POST)
    @ResponseBody
    public int deleteComment(@RequestParam("commentId")String commentId){
        int id = Integer.parseInt(commentId.trim());
        logger.info("=====delete comment id:{}", commentId);
        return commentService.deleteComment(id);
    }

    @RequestMapping(value = "/insertCommentEvaluate", method = RequestMethod.POST)
    @ResponseBody
    public void insertCommentEvaluate(CommentEvaluateDto commentEvaluateDto){
        logger.info("customerId:{}, movieId:{}, content:{}, rating:{}",
                commentEvaluateDto.getCustomerId(), commentEvaluateDto.getMovieId(),
                commentEvaluateDto.getContent(), commentEvaluateDto.getRating());
        CommentDomain commentDomain = new CommentDomain();
        commentDomain.setCustomerId(commentEvaluateDto.getCustomerId());
        commentDomain.setMovieId(commentEvaluateDto.getMovieId());
        commentDomain.setContent(commentEvaluateDto.getContent());
        commentService.insertComment(commentDomain);

        EvaluateDomain evaluateDomain = new EvaluateDomain();
        evaluateDomain.setCustomerId(commentEvaluateDto.getCustomerId());
        evaluateDomain.setMovieId(commentEvaluateDto.getMovieId());
        evaluateDomain.setRating(commentEvaluateDto.getRating());
        evaluateService.insertEvaluate(evaluateDomain);
    }
}
